window.onload = function () {
    var regtel = /^1[34578]\d{9}$/; // 手机号码的正则表达式
    var regqq = /^[1-9]\d{4,}$/; // 10000
    var regnc = /^[\u4e00-\u9fa5]{2,8}$/;
    var regmsg = /^\d{6}$/;
    var regpwd = /^[a-zA-Z0-9_-]{6,16}$/;
    var tel = document.querySelector('#tel');
    var qq = document.querySelector('#qq');
    var nc = document.querySelector('#nc');
    var meg = document.querySelector('#msg');
    var pwd = document.querySelector('#pwd');
    var surepwd = document.querySelector('#surepwd');
    regexp(tel, regtel);
    regexp(qq, regqq);
    regexp(nc, regnc);
    regexp(meg, regmsg)
    regexp(pwd, regpwd);

    function regexp(ele, reg) {
        ele.onblur = function () {
            if (reg.test(this.value)) {
                this.nextElementSibling.className = 'success';
                this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您输入正确'
            } else {
                this.nextElementSibling.className = 'error';
                this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 格式不正确，请从新输入 '
            }
            if (this.id == 'pwd') {
                showPswdLevel(this.value);
            }
        };
    }
    surepwd.onblur = function () {
        if (this.value == pwd.value) {
            this.nextElementSibling.className = 'success';
            this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您输入正确';
        } else {
            this.nextElementSibling.className = 'error'
            this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 两次密码输入不一致';
        }
    }
};

function showPswdLevel(msg) {
    var lvl = 0;
    if (msg.match(/[0-9_]/)) {
        lvl++;
    }
    if (msg.match(/[A-Z]/)) {
        lvl++;
    }
    if (msg.match(/[a-z]/)) {
        lvl++;
    }
    if (document.querySelector('.safe em.current')) {
        document.querySelector('.safe em.current').classList.remove('current');
    }
    document
        .querySelector('.safe em:nth-child(' + lvl + ')')
        .classList.add('current');
}